/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

 
//possible errors
var noNumbersError;
var emailFormatError;
var emptyVarError;

function verifyRegistration(){
    var regForm = document.forms["register"];

    noNumbersError = false;
    emailFormatError = false;
    emptyVarError = false;

    //input vars
    var firstName = regForm.firstName;
    var lastName = regForm.lastName;
    var email = regForm.email;
    var username = regForm.username;
    var password = regForm.password;


    //verify firstName
    if(!verifyNotEmpty(firstName) || !verifyNoNumbers(firstName)){
        firstName.style.backgroundColor = "#FFC1C1";
        firstName.style.border = "2px solid red";
    }else{
        firstName.style.backgroundColor = "white";
        firstName.style.border = "2px inset";
    }

    //verify lastname
    if(!verifyNotEmpty(lastName) || !verifyNoNumbers(lastName)){
        lastName.style.backgroundColor = "#FFC1C1";
        lastName.style.border = "2px solid red";
    }else{
        lastName.style.backgroundColor = "white";
        lastName.style.border = "2px inset";
    }

    //verify email
    if(!verifyNotEmpty(email) || !verifyEmailFormat(email)){
        email.style.backgroundColor = "#FFC1C1";
        email.style.border = "2px solid red";
    }else{
        email.style.backgroundColor = "white";
        email.style.border = "2px inset";
    }

    if(!verifyNotEmpty(username)){
        email.style.backgroundColor = "#FFC1C1";
        email.style.border = "2px solid red";
    }else{
        email.style.backgroundColor = "white";
        email.style.border = "2px inset";
    }

    if(!verifyNotEmpty(password)){
        email.style.backgroundColor = "#FFC1C1";
        email.style.border = "2px solid red";
    }else{
        email.style.backgroundColor = "white";
        email.style.border = "2px inset";
    }


    if(emptyVarError || emailFormatError || noNumbersError){
        var msg = "Please correct the following errors:\n\n";

        if(emptyVarError)
            msg += "- Ensure there are no empty fields.\n\n";
        if(emailFormatError)
            msg += "- Ensure your email is in proper format.\n\n";
        if(noNumbersError)
            msg += "- Please ensure all non-numeric fields do not contain numbers.\n\n";

        alert(msg);
    }else{
        regForm.submit();
    }

}

function verifyNotEmpty(input){
    var text = input.value;
    if(text == null || text.length == 0){
        emptyVarError = true;
        return false;
    }else{
        return true;
    }
}

function verifyNoNumbers(input){

    var text = input.value;

    for(var i = 0; i < text.length; i++){
        if(!isNaN(text.charAt(i))){
            noNumbersError = true;
            return false;
        }
    }
    return true;
}

function verifyEmailFormat(input){
    var text = input.value;

    var atFound = false;

    for(var i = 0; i < text.length; i++){
        if(text.charAt(i) == '@'){
            atFound = true;
        }
    }

    if(atFound){
        return true;
    }else{
        emailFormatError = true;
        return false;
    }
}

